/**
 * @Author: wangliuyang
 * @Date: 2021/9/6 13:38
 * @Description:34. 在排序数组中查找元素的第一个和最后一个位置
 */
public class Solution_34 {
    public int[] searchRange(int[] nums, int target) {
        int frist = findFrist(nums, target);
        int last = findLast(nums,target);
        return new int[]{frist,last};
    }
    public int findFrist(int[] nums,int target){
        int L = 0;
        int R = nums.length - 1;
        int ans = -1;
        while (L <= R){
            int mid = L + ((R - L) >> 2);
            if (nums[mid] > target){
                R = mid - 1;
            }else if (nums[mid] < target){
                L = mid + 1;
            }else {
                ans = mid;
                R = mid - 1;
            }
        }
        return ans;
    }
    public int findLast(int[] nums,int target){
        int L = 0;
        int R = nums.length - 1;
        int ans = -1;
        while (L <= R){
            int mid = L + ((R - L) >> 2);
            if (nums[mid] > target){
                R = mid - 1;
            }else if (nums[mid] < target){
                L = mid + 1;
            }else {
                ans = mid;
                L = mid + 1;
            }
        }
        return ans;
    }
}
